package cn.sciento.core.interceptor;

import java.util.function.Consumer;

public final class InterceptorChain<T> {
  private final ChainId chainId;
  
  private final Consumer<T> preConsumer;
  
  private final Consumer<T> postConsumer;
  
  protected InterceptorChain(ChainId chainId, Consumer<T> preConsumer, Consumer<T> postConsumer) {
    this.chainId = chainId;
    this.preConsumer = preConsumer;
    this.postConsumer = postConsumer;
  }
  
  public final void doExecute(T target, Operation<T> operation) {
    this.preConsumer.accept(target);
    if (operation != null) {
      operation.execute(target);
    }
    this.postConsumer.accept(target);
  }
  
  public ChainId getChainId() {
    return this.chainId;
  }
}
